package com.qikecn.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
 * 时间日期格式化
 * @author wangst
 */
public class DateUtil {
	public static final String FORMAT_MM_DD = "MM-dd";
	
	public static final String FORMAT_YYYY_MM = "yyyy-MM";
	
	public static final String FORMAT_YYYY_MM_DD = "yyyy-MM-dd";
	
	public static final String FORMAT_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
	
	public static final String FORMAT_YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
	
	/*//修改西部数码云服务器系统默认时区的问题
	static{
		TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
	}*/
	
	/**
	 * 获取日期字符串
	 * @param date
	 * @param format
	 * @return
	 */
	public static String getDateStrByFormat(Date date, String format){
		String result = "";
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			result = sdf.format(date);
		} catch (Exception e) {
			// TODO: handle exception
			System.out.println("Exception:"+e);
		}
		return result;
	}
	
	/**
	 * 获取日期字符串
	 * @param milliseconds
	 * @param format
	 * @return
	 */
	public static String getDateStrByFormat(long milliseconds, String format){
		return getDateStrByFormat(new Date(milliseconds), format);
	}
	
	/**
	 * 字符串转日期
	 * @param datetimeStr
	 * @param format
	 * @return
	 */
	public static Date getDateByStr(String datetimeStr, String format){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		Date date = null;
		try {
			date = sdf.parse(datetimeStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return date;
	}
}
